﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Runtime.Remoting.Messaging;
using System.Data.Entity.Core.Mapping;
using System.Data.Entity.Core.Metadata.Edm;

namespace PlatWL.Data
{
    public class DbContextFactory
    {
        public static DbContext GetCurrent()
        {
            DbContext dbContext = CallContext.GetData(typeof(DbContext).FullName) as DbContext;
            if (dbContext == null)
            {
                dbContext = new PlatWLEntities();
                CallContext.SetData(typeof(DbContext).FullName, dbContext);
            }
            return dbContext;
        }

        public static void PreLoadEF()
        {
            using (var dbcontext = GetCurrent())
            {
                var objectContext = ((IObjectContextAdapter)dbcontext).ObjectContext;
                var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
                mappingCollection.GenerateViews(new List<EdmSchemaError>());
            }

        }


    }
}
